package bb.lanxing.util.pop;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.concurrent.PriorityBlockingQueue;

import bb.lanxing.util.Log;


public class PopManager {
    private static PopManager INSTANCE = null;
    private static int MSG_DISMISS = 2;
    private static int MSG_SHOW = 1;
    private static PopDialog popDialogCurrent;
    private PriorityBlockingQueue<PopDialog> queue = new PriorityBlockingQueue<>();
    private IncomingHandler handler = new IncomingHandler(Looper.getMainLooper(), this);

    public static class IncomingHandler extends Handler {
        private final WeakReference<PopManager> popManger;

        IncomingHandler(Looper looper, PopManager popManager) {
            super(looper);
            this.popManger = new WeakReference<>(popManager);
        }

        @Override
        public void handleMessage(Message message) {
            try {
                PopDialog popDialog = (PopDialog) message.obj;
                if (this.popManger != null && popDialog != null) {
                    if (message.what != PopManager.MSG_SHOW || popDialog.getDialog() == null || popDialog.getContext() == null || popDialog.getContext().isFinishing()) {
                        if (message.what == PopManager.MSG_DISMISS && popDialog.getDialog() != null && popDialog.getContext() != null && !popDialog.getContext().isFinishing() && popDialog.getDialog().isShowing()) {
                            popDialog.getDialog().dismiss();
                            if (popDialog.getDialogListener() != null) {
                                popDialog.getDialogListener().onDismiss();
                            }
                        }
                    } else if (!popDialog.getDialog().isShowing()) {
                        popDialog.getDialog().show();
                        if (popDialog.getDialogListener() != null) {
                            popDialog.getDialogListener().onShow();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void showDialog(PopDialog popDialog) {
        popDialogCurrent = popDialog;
        Message message = new Message();
        message.obj = popDialog;
        message.what = MSG_SHOW;
        this.handler.sendMessage(message);
        Log.d("PopManager", "showDialog" + popDialog.getContext() + "| popDialog getPriority" + popDialog.getPriority());
    }

    private void dismissDialog(PopDialog popDialog) {
        popDialogCurrent = null;
        Message message = new Message();
        message.obj = popDialog;
        message.what = MSG_DISMISS;
        this.handler.sendMessage(message);
        Log.d("PopManager", "dismissDialog" + popDialog.getContext() + "| popDialog getPriority" + popDialog.getPriority());
    }

    public static PopManager getInstance() {
        if (INSTANCE == null) {
            synchronized (PopManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new PopManager();
                }
            }
        }
        return INSTANCE;
    }

    public boolean isAlreadyInQueue(PopDialog popDialog) {
        Iterator<PopDialog> it = this.queue.iterator();
        while (it.hasNext()) {
            if (it.next().getUid() == popDialog.getUid()) {
                return true;
            }
        }
        return false;
    }

    public void pushToQueue(PopDialog popDialog) {
        if (isAlreadyInQueue(popDialog)) {
            return;
        }
        this.queue.add(popDialog);
    }

    public void removeTop() {
        this.queue.poll();
    }

    public void clear() {
        PriorityBlockingQueue<PopDialog> priorityBlockingQueue = this.queue;
        if (priorityBlockingQueue != null && priorityBlockingQueue.size() > 0) {
            this.queue.clear();
        }
        dismissCurrent();
    }

    public void dismissCurrent() {
        PopDialog popDialog = popDialogCurrent;
        if (popDialog != null) {
            dismissDialog(popDialog);
        }
        removeTop();
        showNextPopDialog();
    }

    public boolean isOutOfDialogDate(PopDialog popDialog) {
        long currentTimeMillis = System.currentTimeMillis();
        return popDialog.getStartDate() > 0 && popDialog.getEndDate() > 0 && currentTimeMillis > popDialog.getStartDate() && currentTimeMillis < popDialog.getEndDate();
    }

    public void showNextPopDialog() {
        PopDialog element;
        PriorityBlockingQueue<PopDialog> priorityBlockingQueue = this.queue;
        if (priorityBlockingQueue == null || priorityBlockingQueue.isEmpty()) {
            return;
        }
        PopDialog popDialog = popDialogCurrent;
        if ((popDialog == null || !popDialog.getDialog().isShowing()) && (element = this.queue.element()) != null) {
            if (isOutOfDialogDate(element)) {
                removeTop();
                showNextPopDialog();
                return;
            }
            showDialog(element);
        }
    }
}
